//
//  VRPushViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/11.
//

import UIKit

class VRWelcomeViewController: VRBaseViewController {

    private let bgImageView = c_make_ex(UIImageView.self)
        .image("welcome_bg").end()
    private let appleLogin          = UIIconButton(style: .leading(itemSpace: 10))
    private let loginButton         = c_make_ex(UIButton.self)
        .bgColor("#FFFC00").corner(25).font(18.font_bold)
        .text("Log In").color(.black).end()
    private let setupButton         = c_make_ex(UIButton.self)
        .bgColor(.clear).corner(25).font(18.font_bold).border("#FFFC00".toColor, width: 2)
        .text("Sign Up").color("#FFFC00".toColor).end()
    private lazy var policyLabel        = ActiveLabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            bgImageView.subviews{
                appleLogin
                loginButton
                setupButton
                policyLabel
            }
        }
        
        bgImageView.fillContainer()
        appleLogin.top(.navi_height + 480).leading(20).trailing(20).height(50)
        loginButton.leading(20).trailing(20).height(50)
        loginButton.Top == appleLogin.Bottom + 15
        setupButton.leading(20).trailing(20).height(50)
        setupButton.Top == loginButton.Bottom + 15
        policyLabel.centerHorizontally()
        policyLabel.Top == setupButton.Bottom + 30
        
        appleLogin.style { v in
            v.setImage("welcome_apple".toImage, for: .normal)
            v.iconSize = CGSize(width: 24, height: 24)
            v.setTitle("Sign In With Apple", for: .normal)
            v.setTitleColor(.black, for: .normal)
            v.titleLabel?.font = 18.font_bold
            v.backgroundColor = .white
            v.layer.cornerRadius = 25
            v.layer.masksToBounds = true
        }
        
        configTermsAndPolicyStyle()
        bgImageView.isUserInteractionEnabled = true
        appleLogin.addTarget(self, action: #selector(clickAppleButton), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        setupButton.addTarget(self, action: #selector(clickSetupButton), for: .touchUpInside)
        
        for family: String in UIFont.familyNames
                        {
                            print("\(family)")
                            for names: String in UIFont.fontNames(forFamilyName: family)
                            {
                                print("== \(names)")
                            }
                        }
    }
    
    private func configTermsAndPolicyStyle() {
        let customType1 = ActiveType.custom(pattern: "Terms of Use")
        let customType2 = ActiveType.custom(pattern: "Privacy Policy")
        policyLabel.isShowUnderLine = true
        policyLabel.font = 14.font_medium
        policyLabel.numberOfLines = 0
        policyLabel.textAlignment = .center
        policyLabel.enabledTypes = [customType1, customType2]
        policyLabel.text = "Terms of Use & Privacy Policy"
        policyLabel.textColor = "#FFFFFF".toColor.alpha(0.6)
        policyLabel.customColor[customType1] = .white
        policyLabel.customColor[customType2] = .white
        
        policyLabel.handleCustomTap(for: customType1) { [weak self] str in
            
        }
        policyLabel.handleCustomTap(for: customType2) { [weak self] str in
            
        }
    }

}

//MARK: action
extension VRWelcomeViewController {
    
    @objc private func clickLoginButton(){
        
        self.navigationController?.pushViewController(VRLoginViewController(), animated: true)
    }
    
    @objc private func clickSetupButton(){
        
        self.navigationController?.pushViewController(VRSetupViewController(), animated: true)
    }
    
    @objc private func clickAppleButton(){
        
        self.navigationController?.pushViewController(VRUserInfoViewController(), animated: true)
        
        AppleLoginTool.instence.show { user, token in
            RT.appleLogininWith(emailNum: user, Password: "") { info in
                ToastTool.dismiss()
                if let sym = info.info9, sym.count > 0 {
                    let str = GetInfo(key: "UserInfo") as! NSDictionary
                    let jsonData = InfoUtil.initInfo.deserialize(from: str)
                    USEINFOMODEL = jsonData!
                    UIApplication.window?.rootViewController = VRTabBarViewController()
                } else {
                    //新用户
                    let vc = VRUserInfoViewController()
                    UIApplication.topController()?.navigationController?.pushViewController(vc, animated: true)
                }

            }
        } failure: { error in
            error.errorDescripton.toast_error
        }
    }
}
